<script lang="jsx">
import { finishCourt } from '@/api/court'
import ConfigForm from '@/components/CRUD/ConfigForm.vue'
import { ElMessage } from 'element-plus'

export default {
  components: {
    ConfigForm,
  },
  props: {
    info: {
      default: () => ({}),
      type: Object,
    },
  },
  emits: ['success', 'close'],
  setup(props, { emit }) {
    const createForm = [
      {
        label: '上传判决书',
        key: 'pj_result_url',
        type: 'file',
        props: {
          autoUpload: true,
          accept: '.pdf,.doc,.docx',
        },
      },
      {
        label: '上传调解书',
        key: 'tj_result_url',
        type: 'file',
        props: {
          autoUpload: true,
          accept: '.pdf,.doc,.docx',
        },
      },
    ]
    const onClose = () => {
      emit('close')
    }
    const onSuccess = () => {
      emit('success')
    }

    return () => (
      <>
        <ConfigForm
          rowKey="open_court_id"
          configs={createForm}
          server={{ post: finishCourt }}
          onSuccess={onSuccess}
          onClose={onClose}
          beforeSubmit={(params) => {
            if (
              !params.pj_result_url &&
              !params.tj_result_url
            ) {
              ElMessage.error('请至少上传一个判决书或调解书')
              throw new Error('请至少上传一个判决书或调解书')
            }
            params.open_court_id = props.info.id
            return params
          }}
        ></ConfigForm>
      </>
    )
  },
}
</script>
